import 'package:meta/meta.dart';

import 'api_exception.dart';

@immutable
class ResponseResult<T> {
  final dynamic value;

  ResponseResult({@required this.value});

  bool get isSuccess => value is! Failure;

  bool get isFailure => value is Failure;

  T get getOrNull => isFailure ? null : value as T;

  ApiException get exceptionOrNull =>
      isFailure ? (value as Failure).exception : null;

  static ResponseResult<T> success<T>(T value) => ResponseResult(value: value);

  static ResponseResult<T> failure<T>(Exception e) =>
      ResponseResult(value: Failure(exception: ApiException.parseExcetion(e)));

  static ResponseResult<T> failure1<T>(ApiException e) =>
      ResponseResult(value: Failure(exception: e));
  
  static ResponseResult<T> failure2<T>(Error e) =>
      ResponseResult(value: Failure(exception: ApiException.parseError(e)));
}

@immutable
class Failure {
  final ApiException exception;

  Failure({@required this.exception});
}
